package com.lightheart.sphr.xiaobinggan.customchannel

import android.annotation.SuppressLint
import android.graphics.Color
import android.text.TextUtils
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.cheng.channel.Channel
import com.cheng.channel.ViewHolder
import com.cheng.channel.adapter.BaseStyleAdapter
import com.cheng.channel.adapter.ChannelListenerAdapter
import com.lightheart.sphr.xiaobinggan.R
import com.lightheart.sphr.xiaobinggan.base.BaseActivity
import com.lightheart.sphr.xiaobinggan.module.home.presenter.HomePresenter
import com.lightheart.sphr.xiaobinggan.timeselection.uitls.CustomToastUtils
import kotlinx.android.synthetic.main.activity_custom_channel.*
import kotlinx.android.synthetic.main.common_toolbar.*
import java.util.*

class CustomChannelActivity : BaseActivity<HomePresenter>() {

    private val TAG = "CustomChannelActivity:"

    private val data = LinkedHashMap<String, List<Channel>>()

    override fun getLayoutId() = R.layout.activity_custom_channel

    override fun initInjector() {
    }

    override fun initView() {

        initToolbar(common_toolbar, common_toolbar_title_tv, bt_sub, R.string.custom_channel_view, false, R.string.register)

        val myChannel = arrayOf("要闻国家", "要闻", "要闻", "娱乐", "体育", "军事", "NBA", "国际", "科技", "财经", "汽车", "电影", "游戏", "独家", "房产",
                "图片", "时尚", "呼和浩特")
        val recommendChannel1 = arrayOf("综艺", "美食", "育儿", "要闻要家大", "必读", "政法网事", "都市",
                "NFL", "韩流")
        val recommendChannel2 = arrayOf("问答", "文化", "要闻国家大", "股票", "动漫", "理财", "情感", "职场", "旅游")
        val recommendChannel3 = arrayOf("家居", "电竞", "数码", "星座", "教育", "美容", "电视剧",
                "搏击", "健康")

        val myChannelList: MutableList<Channel> = ArrayList()
        val recommendChannelList1: MutableList<Channel> = ArrayList()
        val recommendChannelList2: MutableList<Channel> = ArrayList()
        val recommendChannelList3: MutableList<Channel> = ArrayList()

        for (i in myChannel.indices) {
            val aMyChannel = myChannel[i]
            var channel: Channel
            channel = if (i > 2 && i < 6) {
                //可设置频道归属板块（channelBelong），当前设置此频道归属于第二板块，当删除该频道时该频道将回到第二板块
                Channel(aMyChannel, 2, i)
            } else if (i > 7 && i < 10) {
                //可设置频道归属板块（channelBelong），当前设置此频道归属于第三板块，当删除该频道时该频道将回到第三板块中
                Channel(aMyChannel, 3, i)
            } else {
                Channel(aMyChannel, i as Any)
            }
            myChannelList.add(channel)
        }

        for (aMyChannel in recommendChannel1) {
            val channel = Channel(aMyChannel)
            recommendChannelList1.add(channel)
        }

        for (aMyChannel in recommendChannel2) {
            val channel = Channel(aMyChannel)
            recommendChannelList2.add(channel)
        }

        for (aMyChannel in recommendChannel3) {
            val channel = Channel(aMyChannel)
            recommendChannelList3.add(channel)
        }

        data["我的频道"] = myChannelList
        data["推荐频道"] = recommendChannelList1
        data["国内"] = recommendChannelList2
        data["国外"] = recommendChannelList3

        channelView.setChannelFixedCount(3) //设置频道不可点击的个数
        channelView.setInsertRecommendPosition(6)
        channelView.setStyleAdapter(MyAdapter())
        channelView.setOnChannelListener(object : ChannelListenerAdapter() {
            override fun channelItemClick(position: Int, channel: Channel) {
                Log.i(TAG, "$position..$channel")
                CustomToastUtils.imgToast("item$position$channel", "666")
            }

            @SuppressLint("LongLogTag")
            override fun channelEditStateItemClick(position: Int, channel: Channel) {
                Log.i(TAG + "EditState:", "$position..$channel")
            }

            override fun channelEditFinish(channelList: List<Channel>) {
                Log.i(TAG, channelList.toString())
                Log.i(TAG, channelView.isChange().toString() + "")
                Log.i(TAG, channelView.getOtherChannel().toString())
                CustomToastUtils.imgToast("完成","5555")
            }

            override fun channelEditStart() {
                Log.i(TAG, "channelEditStart")
                CustomToastUtils.imgToast("编辑","5555")
            }
        })
    }

    private inner class MyAdapter : BaseStyleAdapter<MyAdapter.MyViewHolder>() {
        override fun createStyleView(parent: ViewGroup, channelName: String): MyViewHolder {
            val inflate: View = LayoutInflater.from(parent.context).inflate(R.layout.layout_custom_channel, null)
            val customViewHolder = MyViewHolder(inflate)
            customViewHolder.tv.text = channelName
            return customViewHolder
        }

        override fun getChannelData(): LinkedHashMap<String, List<Channel>> {
            return data
        }

        override fun setNormalStyle(viewHolder: MyViewHolder) {//设置正常状态下频道样式
            viewHolder.tv.setBackgroundResource(R.drawable.bg_channel_custom_normal)
            val iterator = data.entries.iterator()
            while (iterator.hasNext()) {
                val entry = iterator.next() as Map.Entry<*, *>
                println("map_key" + entry.key + "value" + entry.value)
                if (TextUtils.equals(entry.key.toString(),"我的频道")) {
                    viewHolder.iv.visibility = View.GONE
                } else {
                    viewHolder.iv.visibility = View.VISIBLE
                    viewHolder.iv.setBackgroundDrawable(resources.getDrawable(R.mipmap.icon_channel_jj))
                }
            }
        }

        override fun setFixedStyle(viewHolder: MyViewHolder) {//设置固定状态下频道样式
            viewHolder.tv.setTextColor(Color.parseColor("#98999A"))
            viewHolder.tv.setBackgroundResource(R.drawable.bg_channel_custom_fixed)
        }

        override fun setEditStyle(viewHolder: MyViewHolder) {//设置编辑状态下频道样式
            viewHolder.tv.setBackgroundResource(R.drawable.bg_channel_custom_edit)
            viewHolder.iv.visibility = View.VISIBLE
            viewHolder.iv.setBackgroundDrawable(resources.getDrawable(R.mipmap.icon_channel_cross))
        }

        override fun setFocusedStyle(viewHolder: MyViewHolder) {//设置获取焦点（拖拽）状态下频道样式
            viewHolder.tv.setBackgroundResource(R.drawable.bg_channel_custom_focused)
        }

        inner class MyViewHolder(itemView: View) : ViewHolder(itemView) {
            var tv: TextView = itemView.findViewById(R.id.tv_channel)
            var iv: ImageView = itemView.findViewById(R.id.iv_delete)
        }
    }
}